package com.mcr.graduateprogram.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.mcr.graduateprogram.common.JacksonObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

@Configuration
@EnableSwagger2
@EnableKnife4j
public class WebMvcConfig implements WebMvcConfigurer {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.mcr.graduateprogram.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo(){//描述接口文档
        return new ApiInfoBuilder()
                .title("steam激活码商店")
                .version("1.0")
                .description("接口文档")
                .build();
    }
    @Override //静态资源映射
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    /**
     *扩展mvc框架消息转换器
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter messageConverter=new MappingJackson2HttpMessageConverter();
        //创建消息转换器对象
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //设置对象转换器,底层用jackson将Java-->json
        converters.add(0,messageConverter);
        //将上面的消息转换器对象追加到mvc框架的转换器集合中,索引为0，放在最前面优先使用
    }
}

